<?php
/*------------------------------------------------------------------------
 # com_k2store - K2 Store v 2.6
# ------------------------------------------------------------------------
# author    Ramesh Elamathi - Weblogicx India http://www.weblogicxindia.com
# copyright Copyright (C) 2012 Weblogicxindia.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://k2store.org
# Technical Support:  Forum - http://k2store.org/forum/index.html
-------------------------------------------------------------------------*/


// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );
jimport('joomla.html.parameter');

class plgSystemK2Store extends JPlugin {

	function plgSystemK2Store( &$subject, $config ){
		parent::__construct( $subject, $config );
		$this->_plugin = JPluginHelper::getPlugin( 'system', 'k2store' );
		$params = new JRegistry;
		$params->loadString($this->_plugin->params );
		$this->_params = $params;

		//load default language
		$lang = JFactory::getLanguage();
		$lang->load('com_k2store', JPATH_SITE);
	}

	function onAfterRoute() {

		$mainframe = &JFactory::getApplication();
		require_once (JPATH_SITE.'/components/com_k2store/helpers/modules.php');
		JHtml::_('behavior.framework');
		JHtml::_('behavior.modal');
		$baseURL = JURI::root();
		$k2storeparams = &JComponentHelper::getParams('com_k2store');
		$document =& JFactory::getDocument();
		$document->addScriptDeclaration("var k2storeURL = '$baseURL';");
		if($mainframe->isAdmin()) {
			$document->addScript($baseURL.'media/k2store/js/k2store_admin.js');
			$document->addStyleSheet($baseURL.'media/k2store/css/k2store_admin.css');

			//add additional css if it is version 2.5
			if (!version_compare(JVERSION, '3.0', 'ge'))
			{
				$document->addStyleSheet($baseURL.'media/k2store/css/k2store_admin_no_bootstrap.css');
			}
		}
		else {
			if(version_compare(JVERSION, '3.0', 'ge')) {
				//load jquery if version is 3.0
				// Joomla! 3.0 and later, use Joomla! code to load the library
				JHtml::_('jquery.framework');
			}else {
				//load jquery for joomla 2.5/ local/remote
				if($k2storeparams->get('k2store_load_jquery', 1) == 1)
					//load from local
					$document->addScript($baseURL.'media/k2store/js/jquery.min.js');
				elseif($k2storeparams->get('k2store_load_jquery', 1) == 2)
				//load from remote
				$document->addScript('https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js');
			}
				
			$script = "
				if(typeof(K2Store) == 'undefined') {
					var K2Store = jQuery.noConflict();
				}";
		$document->addScriptDeclaration($script);
		$document->addScript(JURI::root(true).'/media/k2store/js/jquery.validate.min.js');
		//choose what library to load
		$document->addScript($baseURL.'media/k2store/js/k2store.js');

		// Add related CSS to the <head>
		if ($document->getType() == 'html' && $k2storeparams->get('k2store_enable_css')) {

			jimport('joomla.filesystem.file');

			// k2store.css
			if(JFile::exists(JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'css'.DS.'k2store.css'))
				$document->addStyleSheet($baseURL.'templates/'.$mainframe->getTemplate().'/css/k2store.css');
			else
				$document->addStyleSheet($baseURL.'media/k2store/css/k2store.css');
			if (!version_compare(JVERSION, '3.0', 'ge'))
			{
				$document->addStyleSheet($baseURL.'media/k2store/css/k2store_no_bootstrap.css');
			}

		} else {
			$document->addStyleSheet($baseURL.'media/k2store/css/k2store.css');
				
			if (!version_compare(JVERSION, '3.0', 'ge'))
			{
				$document->addStyleSheet($baseURL.'media/k2store/css/k2store_no_bootstrap.css');
			}
		}
			
		$this->_addCartJS();

		}  //end of if condition


	} //end of function

	function _addCartJS() {

		$valid_ok = JText::_('K2STORE_VALIDATION_OK');
		$processing = JText::_('K2STORE_PROCESSING');
		$script ='';
		$script .="
		K2Store(document).ready(function(){
			K2Store('.k2storeCartForm').each(function(){
				K2Store(this).validate({
						errorElement: 'em',
						errorPlacement: function(error, element) {
							error.appendTo( element.parent().parent().next('div'));
						},
						success: function(label) {
							label.text('$valid_ok').addClass('success');
						},
						submitHandler: function(form) {
							";
								$k2storeparams = &JComponentHelper::getParams('com_k2store');
								if($k2storeparams->get('popup_style') == 3) {
									$script .="form.submit();";
								}else {
									$script .="
									k2storeAddToCart( 'addtocart', form);
									";
								}
				
						$script .="
					 }
			});
		});
	});	";
		$document =& JFactory::getDocument();
		$document->addScriptDeclaration($script);
	}

}
